﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ContosoUniversity.Models;
using ContosoUniversity.DAL;
using Webdiyer.WebControls.Mvc;

namespace ContosoUniversity.Controllers
{
    public class StudentController : Controller
    {
        private SchoolContext db = new SchoolContext();

        //
        // GET: /Student/

        //public ActionResult Index()
        //{
        //    return View(db.Students.ToList());
        //}

        //
        //GET: /
        public ViewResult Index(string sortOrder,string searchString,int? page=1)
        {
            ViewBag.NameSortParm = string.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";
            var students = from s in db.Students select s;
            if (!string.IsNullOrEmpty(searchString))
            {
                students = students.Where(s=>s.LastName.ToUpper().Contains(searchString.ToUpper())
                    ||s.FirstMidName.ToUpper().Contains(searchString.ToUpper()));
                ViewBag.Filter = searchString;
            }
            switch (sortOrder)
            { 
                case "Name desc":
                    students = students.OrderByDescending(s=>s.LastName);
                    break;
                case "Date":
                    students = students.OrderBy(s=>s.EnrollmentDate);
                    break;
                case "Date desc":
                    students = students.OrderByDescending(s=>s.EnrollmentDate);
                    break;
                default:
                    students = students.OrderBy(s => s.LastName);
                    break;
            }
            int pageSize = 3;
            int pageIndex = page ?? 1;
            return View(students.ToPagedList(pageIndex, pageSize));
        }
        //
        // GET: /Student/Details/5

        public ActionResult Details(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // GET: /Student/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Student/Create

        [HttpPost]
        public ActionResult Create(Student student)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Students.Add(student);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (Exception)
            {
                
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return View(student);
        }

        //
        // GET: /Student/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // POST: /Student/Edit/5

        [HttpPost]
        public ActionResult Edit(Student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }

        //
        // GET: /Student/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        //
        // POST: /Student/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            if (Request.IsAjaxRequest())
            {
                Student student = db.Students.Find(id);
                db.Students.Remove(student);
                int result = db.SaveChanges();
                return Content(result.ToString());
            }
            else
            {
                return Content("-1");
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}